package com.clj.blesample.operation;


import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.clj.blesample.R;
import com.clj.fastble.BleManager;
import com.clj.fastble.data.BleDevice;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class CharacteristicFragment extends Fragment {

    private List<BluetoothGattCharacteristic> characteristicList = new ArrayList<>();
    private ViewPager vpOperation;
    private TabLayout tabLayout;

    public static CharacteristicFragment newInstance() {
        Bundle args = new Bundle();
        CharacteristicFragment fragment = new CharacteristicFragment();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_characteric, null);
        initView(v);
        return v;
    }

    private void initView(View v) {
        vpOperation = v.findViewById(R.id.vpOperation);
        tabLayout = v.findViewById(R.id.tabLayout);
        showData();
    }

    public void showData() {
        BleDevice bleDevice = ((DetailActivity) getActivity()).getBleDevice();
        BluetoothGatt gatt = BleManager.getInstance().getBluetoothGatt(bleDevice);

        BluetoothGattService connectService = null;
        for (BluetoothGattService service : gatt.getServices()) {
            if (service.getUuid().toString().startsWith("0000ffa0")) {
                connectService = service;
            }
        }
        if (connectService == null) {
            Toast.makeText(requireContext(), "未找到对应0000ffa0-0000-1000-8000-00805f9b34fb的蓝牙服务", Toast.LENGTH_LONG).show();
            return;
        }
        characteristicList.clear();
        characteristicList.addAll(connectService.getCharacteristics());
        final List<Fragment> fragments = new ArrayList<>();
        final List<String> titles = new ArrayList<>();
        for (BluetoothGattCharacteristic characteristic : characteristicList) {
            if (characteristic.getUuid().toString().startsWith("0000ffa1")) {
                int charaProp = characteristic.getProperties();
                if ((charaProp & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0) {
                    fragments.add(CharacteristicOperationPageFragment.newInstance(characteristic, CharacteristicOperationFragment.PROPERTY_WRITE_NO_RESPONSE));
                    titles.add("Write");
                }
                if ((charaProp & BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                    fragments.add(CharacteristicOperationPageFragment.newInstance(characteristic, CharacteristicOperationFragment.PROPERTY_NOTIFY));
                    titles.add("Notify");
                }
            }
        }
        if (!titles.contains("Notify")) {
            Toast.makeText(requireContext(), "未找到Notify特征:0000ffa1-0000-1000-8000-00805f9b34fb", Toast.LENGTH_LONG).show();
        }
        if (!titles.contains("Write")) {
            Toast.makeText(requireContext(), "未找到Write特征:0000ffa1-0000-1000-8000-00805f9b34fb", Toast.LENGTH_LONG).show();
        }
        vpOperation.setOffscreenPageLimit(characteristicList.size());
        vpOperation.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titles.get(position);
            }
        });
        tabLayout.setupWithViewPager(vpOperation);
        vpOperation.setCurrentItem(0);
    }
}
